import numpy as np
from skimage import io, img_as_float
from skimage.metrics import structural_similarity as ssim
from scipy.signal import correlate2d

def calculate_ncc(image1, image2):
    """ 计算两张图片的归一化互相关系数（NCC） """
    mean1 = np.mean(image1)
    mean2 = np.mean(image2)
    ncc = correlate2d(image1 - mean1, image2 - mean2, mode='same')
    norm = np.sqrt(correlate2d(image1 - mean1, image1 - mean1, mode='same')[0, 0] *
                   correlate2d(image2 - mean2, image2 - mean2, mode='same')[0, 0])
    return ncc / norm

# 读取两张图片
# image1 = img_as_float(io.imread('C:/Users/crxc/Pictures/wandb/1out/fixed.png.tif', as_gray=True))
image1 = img_as_float(io.imread('C:/Users/crxc/Pictures/wandb/fixed.png', as_gray=True))
image2 = img_as_float(io.imread('C:/Users/crxc/Pictures/wandb/label_wrap.png', as_gray=True))
# image2 = img_as_float(io.imread('C:/Users/crxc/Pictures/wandb/1out/moving.png.tif', as_gray=True))

# 计算NCC
# ncc = calculate_ncc(image1, image2)

# 计算SSIM
ssim_value = ssim(image1, image2, data_range=image1.max() - image1.min())

# print("NCC:", ncc)
print("SSIM:", ssim_value)
